﻿


// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Media;
using System.Windows;

namespace SimpleTiles
{
    /// <summary>
    /// Adds extension methods relating to color.
    /// </summary>
    public static class ColorExtensions
    {
        /// <summary>
        /// An array of all the names of the accent colors.
        /// </summary>
        private static string[] _accentColors = { "nokia blue",
                                                  "magenta", 
                                                  "purple",
                                                  "teal", 
                                                  "lime", 
                                                  "brown", 
                                                  "pink", 
                                                  "mango",
                                                  "blue",
                                                  "red",
                                                  "green" };


        /// <summary>
        /// Returns an array of all the names of the accent colors.
        /// </summary>
        public static ReadOnlyCollection<string> AccentColors()
        {
            var currentAccentColorHex = (System.Windows.Media.Color)Application.Current.Resources["PhoneAccentColor"];
            string currentAccentColor = ColorNameFromHex(currentAccentColorHex);

            for (int i = 0; i < _accentColors.Length; i++)
            {
                string s = _accentColors[i];
                if (s.Equals(currentAccentColor))
                {
                    string temp = _accentColors[0];
                    _accentColors[0] = s;
                    _accentColors[i] = temp;
                }
            }

            return new ReadOnlyCollection<string>(_accentColors);
        }

        public static ReadOnlyCollection<string> AccentColors(string selectedColor)
        {
            string currentAccentColor = selectedColor;

            for (int i = 0; i < _accentColors.Length; i++)
            {
                string s = _accentColors[i];
                if (s.Equals(currentAccentColor))
                {
                    string temp = _accentColors[0];
                    _accentColors[0] = s;
                    _accentColors[i] = temp;
                }
            }

            return new ReadOnlyCollection<string>(_accentColors);
        }

        private static string ColorNameFromHex(Color hexColor)
        {
            switch (hexColor.ToString())
            {
                case "#FF1080DD": return "nokia blue";
                case "#FF1BA1E2": return "blue";
                case "#FFA05000": return "brown";
                case "#FF339933": return "green";
                case "#FFE671B8": return "pink";
                case "#FFA200FF": return "purple";
                case "#FFE51400": return "red";
                case "#FF00ABA9": return "teal";
                case "#FF8CBF26": 
                case "#FFA2C139": return "lime";
                case "#FFFF0097":
                case "#FFD80073": return "magenta";
                case "#FFF09609": return "mango";
                default: return "custom eleventh color"; //Manufacturer color
            }
        }

        /// <summary>
        /// Returns a Color for a hex value.
        /// </summary>
        /// <param name="argb">The hex value</param>
        /// <remarks>Calls to this method should look like 0xFF11FF11.ToColor().</remarks>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "argb", Justification ="By design")]
        public static Color ToColor(this uint argb)
        {
            return Color.FromArgb((byte)((argb & 0xff000000) >> 0x18),
                                  (byte)((argb & 0xff0000) >> 0x10),
                                  (byte)((argb & 0xff00) >> 8),
                                  (byte)(argb & 0xff));
        }

        /// <summary>
        /// Returns a SolidColorBrush for a hex value.
        /// </summary>
        /// <param name="argb">The hex value</param>
        /// <remarks>Calls to this method should look like 0xFF11FF11.ToSolidColorBrush().</remarks>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "argb", Justification ="By design")]
        public static SolidColorBrush ToSolidColorBrush(this uint argb)
        {
            return new SolidColorBrush(argb.ToColor());
        }
    }
}
